import pandas as pd
list_of_dates = ['2019-11-20', '2020-01-02', '2020-02-05','2020-03-10','2020-04-16']
employees=['Hisila', 'Shristi','Zeppy','Alina','Jerry']
df = pd.DataFrame({
'Joined date': pd.to_datetime(list_of_dates)}, index=employees)
df['Year'] = df['Joined date'].dt.year
df['Month'] = df['Joined date'].dt.month
print(df)
关键函数:df['Month'] = df['Joined date'].dt.month
筛选满足多条件的数据:
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
path = './0area_jrc_raw'
filenames = os.listdir(path)
for filename in filenames:
file_path = r'./0area_jrc_raw/' + filename
df = pd.read_excel(file_path)
df['Month'] = df['Date'].dt.month
data = df[(df['Month'] >5) & (df['Month']<12)] #select 6-11 month data
print(data['Month'])